import com.ht.aems.communicate.PortListener;
import com.ht.aems.communicate.TcpTransmitPort;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketException;

/**
 * Created by IntelliJ IDEA.
 *
 * @author Dong Shufeng
 *         Date: 2009-2-10
 */
public class SocketClient implements Runnable, PortListener {
    Socket socket = new Socket();
    String host;
    int port;
    private boolean stop = false;
    TcpTransmitPort transport;
    int countTest = 0;

    public SocketClient(String host, int port) {
        try {
            this.host = host;
            this.port = port;
            socket.setSoTimeout(10000);
            transport = new TcpTransmitPort(socket);
            transport.addPortListener(this);
            new Thread(this).start();
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }

    public void sendConnectQuery() {

    }

    public static void main(String[] args) throws IOException {
        SocketClient client = new SocketClient("192.168.1.111", 8401);
        InputStreamReader r = new InputStreamReader(System.in);
        BufferedReader reader = new BufferedReader(r);
        String str;
        while ((str = reader.readLine()) != null) {
            if (str.equalsIgnoreCase("exit"))
                System.exit(0);
            else
                client.transport.send(str.getBytes());
        }
    }

    public void run() {
        int tmp = -1;
        while (!stop) {
            if (tmp == countTest)
                transport.stopAbnormally();
            if (!transport.isOpen())
                try {
                    if (!socket.isConnected()) {
                        socket = new Socket("192.168.1.111", 8401, InetAddress.getLocalHost(), 9688);
                        socket.setSoTimeout(10000);
                    }
                    //if (!socket.isConnected()) {
                    //    InetSocketAddress endpoint = new InetSocketAddress(host, port);
                    //    socket.connect(endpoint);
                    //}
                    transport.setSocket(socket);
                    transport.openPort();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            try {
                tmp = countTest;
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void dataUploaded(byte[] data) {
        System.out.println("received:");

        for (byte b : data) {
            String sTemp = Integer.toHexString(b);
            int nTempLength = sTemp.length();
            if (nTempLength > 2)
                sTemp = sTemp.substring(nTempLength - 2, nTempLength);
            nTempLength = sTemp.length();
            if (nTempLength < 2)
                sTemp = "0" + sTemp;
            System.out.print(sTemp + "\t");
        }
        System.out.println();
        switch (data[0]) {
            case 2://request back
                System.out.println("connect is successful!");
                break;
            case 0x0F: //test code
                System.out.println("test data is received!");
                data[2] = 1;
                transport.send(data);
                if (countTest >= 10000)
                    countTest = 0;
                countTest++;
                break;
            case 0x0E:
                System.out.println("control data is received");
                transport.send(data);
                break;
            default:
                break;
        }
    }

    public void dataSent(byte[] data) {
        System.out.println("send...:");
        for (byte b : data) {
            String sTemp = Integer.toHexString(b);
            int nTempLength = sTemp.length();
            if (nTempLength > 2)
                sTemp = sTemp.substring(nTempLength - 2, nTempLength);
            nTempLength = sTemp.length();
            if (nTempLength < 2)
                sTemp = "0" + sTemp;
            System.out.print(sTemp + "\t");
        }
        System.out.println();
    }
}
